package com.gitee.cui.es;

import java.io.IOException;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

/**
 * ES文档全量查询操作
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022-04-05 15:56
 **/
public class ESDocSearchAll {

	public static void main(String[] args) throws IOException {
		RestHighLevelClient client = new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost", 9200, "http")));

		// 1.查询全量数据信息
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 2.条件查询
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30)));
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 3.分页查询
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
		// // 数值算法：(开始页码-1)*每页条数
		// query.from(2);
		// query.size(2);
		// request.source(query);
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 4.查询排序
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
		// // 排序
		// query.sort("age", SortOrder.DESC);
		// request.source(query);
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 5.查询过滤字段
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
		// // 过滤字段
		// String[] includes = {"name"};
		// String[] excludes = {};
		// query.fetchSource(includes, excludes);
		// request.source(query);
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 6.组合查询
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
		// // boolQuery.must(QueryBuilders.matchQuery("age", 30));
		// // boolQuery.must(QueryBuilders.matchQuery("sex", "男"));
		// // boolQuery.mustNot(QueryBuilders.matchQuery("sex", "男"));
		// boolQuery.should(QueryBuilders.matchQuery("age", 30));
		// boolQuery.should(QueryBuilders.matchQuery("age", 40));
		// SearchSourceBuilder query = new SearchSourceBuilder().query(boolQuery);
		// request.source(query);
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 7.范围查询
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
		// rangeQuery.gte(30);
		// rangeQuery.lte(40);
		// SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQuery);
		// request.source(query);
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 8.模糊查询
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("name", "zhangsan").fuzziness(Fuzziness.ONE);
		// SearchSourceBuilder query = new SearchSourceBuilder().query(fuzzyQuery);
		// request.source(query);
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 9.高亮查询
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "zhangsan");
		//
		// HighlightBuilder highlightBuilder = new HighlightBuilder();
		// highlightBuilder.preTags("<font color='red'>");
		// highlightBuilder.postTags("</font>");
		// highlightBuilder.field("name");
		//
		// SearchSourceBuilder query = new SearchSourceBuilder().query(termQuery).highlighter(highlightBuilder);
		// request.source(query);
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 10.聚合查询
		// SearchRequest request = new SearchRequest();
		// request.indices("user");
		// AvgAggregationBuilder aggregationBuilder = AggregationBuilders.avg("ageAvg").field("age");
		// SearchSourceBuilder query = new SearchSourceBuilder().aggregation(aggregationBuilder);
		// request.source(query);
		// SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		// SearchHits hits = response.getHits();
		// System.out.println(hits.getTotalHits());
		// System.out.println(response.getTook());
		// Aggregations aggregations = response.getAggregations();
		// for (Aggregation aggregation : aggregations) {
		// 	System.out.println(aggregation.getType());
		// }
		// for (SearchHit hit : hits) {
		// 	System.out.println(hit.getSourceAsString());
		// }

		// 11.分组查询
		SearchRequest request = new SearchRequest();
		request.indices("user");
		TermsAggregationBuilder ageGroup = AggregationBuilders.terms("ageGroup").field("age");
		SearchSourceBuilder query = new SearchSourceBuilder().aggregation(ageGroup);
		request.source(query);
		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
		SearchHits hits = response.getHits();
		System.out.println(hits.getTotalHits());
		System.out.println(response.getTook());
		Aggregations aggregations = response.getAggregations();
		for (Aggregation aggregation : aggregations) {
			System.out.println(aggregation.getType());
		}
		for (SearchHit hit : hits) {
			System.out.println(hit.getSourceAsString());
		}

		client.close();
	}
}
